package com.yuxi.controller;

import com.yuxi.entity.Account;
import com.yuxi.service.IAccountService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * controller
 * Created by yuxi on 2017/5/11.
 */
@Api("查询账单信息api")
@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    IAccountService accountService;

    /**
     * 查询all
     *
     * @return
     */
    @ApiOperation("查询所有账单信息")
    @ApiImplicitParams({})
    @ApiResponses(value = {
            @ApiResponse(code = 401, message = "参数错误"),
            @ApiResponse(code = 404, message = "路径不正确"),
            @ApiResponse(code = 501, message = "内部错误"),
            @ApiResponse(code = 200, message = "正常返回")
    })
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Account> getAccounts() {
        return accountService.findAccountList();
    }

    /**
     * 查询 by Id
     *
     * @param id
     * @return
     */
    @ApiOperation("根据账单ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "订单ID",  dataType = "int")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ApiResponses(value = {
            @ApiResponse(code = 401, message = "参数错误"),
            @ApiResponse(code = 404, message = "路径不正确"),
            @ApiResponse(code = 501, message = "内部错误"),
            @ApiResponse(code = 200, message = "正常返回")
    })
    public Account getAccountById(@PathVariable("id") int id) {
        return accountService.findAccountById(id);
    }

    /**
     * 跟新数据
     *
     * @param id
     * @param name
     * @param money
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String updateAccount(@PathVariable("id") int id, @RequestParam(value = "name") String name,
                                @RequestParam(value = "money") double money) {
        Account account = new Account();
        account.setMoney(money);
        account.setName(name);
        account.setId(id);
        int t = accountService.update(account);
        if (t == 1) {
            return account.toString();
        } else {
            return "fail";
        }
    }

    /**
     * 新增
     *
     * @param name
     * @param money
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String postAccount(@RequestParam(value = "name") String name,
                              @RequestParam(value = "money") double money) {
        Account account = new Account();
        account.setMoney(money);
        account.setName(name);
        int t = accountService.add(account);
        if (t == 1) {
            return account.toString();
        } else {
            return "fail";
        }

    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String postAccount(@PathVariable("id") int id) {

        int t = accountService.delete(id);
        return String.valueOf(t);

    }
}
